package KategoriBarang;

import Transaksi.TransaksiForm;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Astri
 */
public class KategoriForm extends javax.swing.JFrame {

    /** Creates new form KategoriForm */
    public KategoriForm() {
        try {
            initComponents();
            simpanButton.setEnabled(false);
            hapusButton.setEnabled(false);

            List<Kategori> kategori = KategoriKontrol.getKoneksiKontrol().getAllKategori();
            KategoriTabelModel model = new KategoriTabelModel(kategori);
            kategoriTable.setModel(model);

        } catch (Exception e) {
            Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        kodeLabel = new javax.swing.JLabel();
        namaLabel = new javax.swing.JLabel();
        kodeKategoriTextField = new javax.swing.JTextField();
        namaKategoriTextField = new javax.swing.JTextField();
        simpanButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        kategoriTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        kembaliButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(204, 204, 255));

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        kodeLabel.setText("Kode Kategori");

        namaLabel.setText("Nama Kategori");

        kodeKategoriTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kodeKategoriTextFieldActionPerformed(evt);
            }
        });
        kodeKategoriTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                kodeKategoriTextFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodeKategoriTextFieldKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                kodeKategoriTextFieldKeyTyped(evt);
            }
        });

        namaKategoriTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                namaKategoriTextFieldActionPerformed(evt);
            }
        });
        namaKategoriTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                namaKategoriTextFieldKeyTyped(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");
        keluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(simpanButton)
                        .addGap(18, 18, 18)
                        .addComponent(hapusButton)
                        .addGap(17, 17, 17)
                        .addComponent(batalButton)
                        .addGap(18, 18, 18)
                        .addComponent(keluarButton)
                        .addGap(133, 133, 133))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(namaLabel)
                            .addComponent(kodeLabel))
                        .addGap(48, 48, 48)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(namaKategoriTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                            .addComponent(kodeKategoriTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                        .addGap(67, 67, 67))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeKategoriTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kodeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(namaLabel)
                    .addComponent(namaKategoriTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hapusButton)
                    .addComponent(batalButton)
                    .addComponent(simpanButton)
                    .addComponent(keluarButton))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        kategoriTable.setAutoCreateRowSorter(true);
        kategoriTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Kode Kategori", "Nama Kategori"
            }
        ));
        kategoriTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                kategoriTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(kategoriTable);

        jLabel5.setText("Daftar Kategori");

        jPanel2.setBackground(new java.awt.Color(204, 204, 255));

        jLabel3.setBackground(new java.awt.Color(204, 204, 255));
        jLabel3.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 24));
        jLabel3.setText("JENIS BARANG");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(168, 168, 168)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(193, 193, 193))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                .addContainerGap())
        );

        kembaliButton.setText("Kembali");
        kembaliButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kembaliButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(413, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(384, Short.MAX_VALUE)
                .addComponent(kembaliButton)
                .addGap(44, 44, 44))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(kembaliButton)
                .addGap(80, 80, 80))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-513)/2, (screenSize.height-474)/2, 513, 474);
    }// </editor-fold>//GEN-END:initComponents

private void namaKategoriTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_namaKategoriTextFieldActionPerformed
// TODO add your handling code here:
    simpanButton.setEnabled(true);
    simpanButton.requestFocus();
    hapusButton.setEnabled(true);
    hapusButton.requestFocus();


}//GEN-LAST:event_namaKategoriTextFieldActionPerformed

private void keluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarButtonActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_keluarButtonActionPerformed

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
// TODO add your handling code here:
    kodeKategoriTextField.setText("");
    namaKategoriTextField.setText("");
    kodeKategoriTextField.setEditable(true);
    simpanButton.setText("Simpan");



}//GEN-LAST:event_batalButtonActionPerformed

private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed

    try {

        String kode = kodeKategoriTextField.getText();
        String nama = namaKategoriTextField.getText();
        Kategori kategoriBaru = new Kategori();
        kategoriBaru.setKodeKategori(kode);
        kategoriBaru.setNamaKategori(nama);

        KategoriKontrol katKontrol = KategoriKontrol.getKoneksiKontrol();
        if (simpanButton.getText() == "Simpan") {

            katKontrol.simpanKategoriStatement(kategoriBaru);
            List<Kategori> kategori = KategoriKontrol.getKoneksiKontrol().getAllKategori();
            KategoriTabelModel model = new KategoriTabelModel(kategori);
            kategoriTable.setModel(model);
            JOptionPane.showMessageDialog(this, "Jenis " + nama + " berhasil disimpan");

        } else {
            katKontrol.updateKategoriStatement(kategoriBaru);
            List<Kategori> kategori = KategoriKontrol.getKoneksiKontrol().getAllKategori();
            KategoriTabelModel model = new KategoriTabelModel(kategori);
            kategoriTable.setModel(model);
            JOptionPane.showMessageDialog(this, "Jenis " + nama + " berhasil diupdate");
        }

    } catch (SQLException ex) {
        Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Data gagal disimpan");

    }
    batalButton.doClick();


}//GEN-LAST:event_simpanButtonActionPerformed

private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
    try {
        String kode = kodeKategoriTextField.getText();
        String nama = namaKategoriTextField.getText();
        Kategori kategoriBaru = new Kategori();
        kategoriBaru.setKodeKategori(kode);
        kategoriBaru.setNamaKategori(nama);

        KategoriKontrol katKontrol = KategoriKontrol.getKoneksiKontrol();
        if (hapusButton.getText() == "Hapus") {
            katKontrol.hapusKategoriStatement(kategoriBaru);

            List<Kategori> kategori = KategoriKontrol.getKoneksiKontrol().getAllKategori();
            KategoriTabelModel model = new KategoriTabelModel(kategori);
            kategoriTable.setModel(model);


            JOptionPane.showMessageDialog(this, "Data dengan nama " + nama + " berhasil dihapus");
        }

    } catch (SQLException ex) {
        Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Data gagal dihapus");
    }

    batalButton.doClick();

}//GEN-LAST:event_hapusButtonActionPerformed

private void kategoriTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_kategoriTableMouseClicked
    int row = kategoriTable.getSelectedRow();
    String kodegroup = kategoriTable.getValueAt(row, 0).toString();
    String namagroup = kategoriTable.getValueAt(row, 1).toString();
    namaKategoriTextField.setText(namagroup);
    kodeKategoriTextField.setText(kodegroup);

    kodeKategoriTextField.setEditable(false);
    namaKategoriTextField.requestFocus();

    simpanButton.setEnabled(true);
    simpanButton.setText("Update");
    hapusButton.setEnabled(true);


}//GEN-LAST:event_kategoriTableMouseClicked

private void kodeKategoriTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kodeKategoriTextFieldActionPerformed
}//GEN-LAST:event_kodeKategoriTextFieldActionPerformed

private void kodeKategoriTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeKategoriTextFieldKeyReleased
    //tanpa dienter kode dimasukkan , nama kategori muncul
    Kategori kategoriCari = new Kategori();
    String kode = kodeKategoriTextField.getText();
    kategoriCari.setKodeKategori(kode);

    try {
        kategoriCari = KategoriKontrol.getKoneksiKontrol().cariKategori(kategoriCari);
        String namaKategori = kategoriCari.getNamaKategori();
        namaKategoriTextField.setText(namaKategori);
    } catch (Exception e) {
        Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, e);

    }
}//GEN-LAST:event_kodeKategoriTextFieldKeyReleased

private void kodeKategoriTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeKategoriTextFieldKeyPressed
    //harus pakai enter baru nama kategori muncul
}//GEN-LAST:event_kodeKategoriTextFieldKeyPressed

private void kodeKategoriTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeKategoriTextFieldKeyTyped
    //harus pakai enter baru nama kategori muncul
    if (evt.getSource().equals(kodeKategoriTextField)) {
        if (kodeKategoriTextField.getText().length() > 2) {
            evt.setKeyChar('\n');
            JOptionPane.showMessageDialog(null, "Karakter Kode maksimal 3");

        }
    }

}//GEN-LAST:event_kodeKategoriTextFieldKeyTyped

private void namaKategoriTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaKategoriTextFieldKeyTyped
    if (!kodeKategoriTextField.getText().isEmpty()) {
        simpanButton.setEnabled(true);
    } else {
        JOptionPane.showMessageDialog(null, "Kode Kategori harus dimasukkan terlebih dahulu");
    }

}//GEN-LAST:event_namaKategoriTextFieldKeyTyped

private void kembaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kembaliButtonActionPerformed
    // TODO add your handling code here:
    new TransaksiForm().setVisible(true);
            this.setVisible(false);
}//GEN-LAST:event_kembaliButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new KategoriForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JButton hapusButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable kategoriTable;
    private javax.swing.JButton keluarButton;
    private javax.swing.JButton kembaliButton;
    private javax.swing.JTextField kodeKategoriTextField;
    private javax.swing.JLabel kodeLabel;
    private javax.swing.JTextField namaKategoriTextField;
    private javax.swing.JLabel namaLabel;
    private javax.swing.JButton simpanButton;
    // End of variables declaration//GEN-END:variables
}
